from typing import List,Union
from nicegui import ui
from nicegui.element import Element


class DiseaseElement(Element):
    def __init__(self, diseases:List[str]=[]):
        super().__init__()
        self.diseases=diseases
        self.inputs:Union[List[ui.input],None]=[]
        # add_disease=ui.button('新增',icon='add_circle', on_click=self.add_element_row).props('dense ')
        self.contains= ui.column(wrap=True).classes('q-pa-sm max-h-52').style('gap:0px')
        if self.diseases:
            for diease_ in self.diseases:
                self.add_element_row(diease_)                


    def add_element_row(self,disease:str=''):
        with self.contains:
            with ui.row().style('gap:0px') as element_row:
                input_=ui.select(['item1','item2'],value=disease,with_input=True).props("dense outlined :input-style={height:'50px'}").classes('w-50 m-0 p-0')
                self.inputs.append(input_)
                ui.button('',icon='delete',on_click=lambda: self.delete_element_row(element_row,input_)).props('dense').classes('size-4')
                ui.button('',icon='arrow_upward',on_click=lambda: self.move_up(input_)).props('dense')
                ui.button('',icon='arrow_downward', on_click=lambda:self.move_down(input_)).props('dense')

    def delete_element_row(self,row:ui.row,input:ui.input):
        row.delete()
        self.inputs.remove(input)

    def move_up(self,input:ui.input):
        index=self.inputs.index(input)
        if index>0:
            self.inputs[index].value,self.inputs[index-1].value=self.inputs[index-1].value,self.inputs[index].value
            self.inputs[index-1].update()
            self.inputs[index].update()
            # print(inputs[index-1].value,inputs[index].value)

    def move_down(self,input:ui.input):
        index=self.inputs.index(input)
        if index<len(self.inputs)-1:
            self.inputs[index].value,self.inputs[index+1].value=self.inputs[index+1].value,self.inputs[index].value
            self.inputs[index+1].update()
            self.inputs[index].update()
            # print(self.inputs[index+1].value,self.inputs[index].value)
